package com.example.dinghao.fragmenttest_312dh.model;

import com.example.dinghao.fragmenttest_312dh.bean.ArticleBean;
import com.example.dinghao.fragmenttest_312dh.iface.ArticleListener;
import com.example.dinghao.fragmenttest_312dh.iface.Articleiface;
import com.example.dinghao.fragmenttest_312dh.service.ArticleService;
import com.google.gson.Gson;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dinghao on 2017/4/20.
 */
//获取文章数据
public class ArticleModel implements Articleiface{
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    public ArticleModel()
    {
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();

    }
    @Override
    public void getResultList(String mod, int page, String SessionID, final ArticleListener articleListener) {
        ArticleService articleService=retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call=articleService.getResult(mod,page,SessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful()&& response.body()!=null)
                    articleListener.onResponse(response.body());
                else
                    articleListener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                articleListener.onFail(t.toString());
            }
        });

    }

}
